import { wechat } from '../api'
import config from '../config'
import { parse } from 'url'
async function signature(ctx, next) {
  let url = ctx.query.url
  if (!url) ctx.throw('404')
  const params = await wechat.getSignature(url)
  ctx.body = {
    success: true,
    params: params
  }
}
async function redirect(ctx, next) {
  const target = config.SITE_ROOT_PATH + '/test'
  const scope = 'snsapi_userinfo'
  const { state } = ctx.query
  const url = wechat.toAuthorizeUrl(scope, target, state)
  ctx.redirect(url)
}
async function authorize(ctx, next) {
  let url = decodeURIComponent(ctx.query.url)
  let params = parse(url, true).query
  let code = params.code
  const user = await wechat.getUserByCode(code)
  ctx.body = {
    success: true,
    data: user
  }
}
export default {
  signature,
  redirect,
  authorize
}
